package com.xu.system.model.convert;

import com.xu.base.utils.LocalDateUtils;
import com.xu.mybatis.model.PageInfo;
import com.xu.security.model.CurrentUser;
import com.xu.system.model.entity.SysUser;
import com.xu.system.model.pojo.user.AddUserReq;
import com.xu.system.model.pojo.user.UserSimpleVo;
import com.xu.system.model.pojo.user.UserTranslateBo;
import com.xu.system.model.pojo.user.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户对象转换器
 *
 * @Author: 许乾伟
 * @Date: 2024/12/3 15:56
 */
@Mapper(componentModel = "spring")
public interface UserConvert {
    @Mappings({
            @Mapping(source = "id", target = "userId")
    })
    CurrentUser toCurrentUser(SysUser data);

    @Mappings({
            @Mapping(target = "birthday", expression = "java(getBirthday(data.getBirthday()))"),
            @Mapping(target = "createBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "createTime", expression = "java(java.time.LocalDateTime.now())"),
            @Mapping(target = "updateBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "updateTime", expression = "java(java.time.LocalDateTime.now())"),
    })
    SysUser toSysUser(AddUserReq data);

    @Mappings({
            @Mapping(source = "id", target = "userId")
    })
    UserVo toUserVo(SysUser data);

    @Mappings({
            @Mapping(source = "id", target = "userId"),
            @Mapping(target = "sex", expression = "java(getSex(data.getSex()))"),
            @Mapping(target = "headImageFileUrl", expression = "java(getHeadImageFileUrl(data.getId(), translate.getHeadImageMap()))"),
            @Mapping(target = "statusStr", expression = "java(com.xu.base.enums.StatusEnum.getByStatus(data.getStatus()))"),
            @Mapping(target = "deptName",expression = "java(getDeptName(data.getId(),translate.getDeptNameMap()))"),
            @Mapping(target = "birthday",expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmdd(data.getBirthday()))")
    })
    UserSimpleVo toUserSimpleVo(SysUser data, @Context UserTranslateBo translate);
    List<UserSimpleVo> toUserSimpleVoList(List<SysUser> list, @Context UserTranslateBo translate);

    PageInfo<UserSimpleVo> toUserSimpleVoPage(PageInfo<SysUser> pageData, @Context UserTranslateBo translate);


    /**
     * 用户头像关联
     * @param userId
     * @param userHeadImageMap
     * @return
     */
    default String getHeadImageFileUrl(Long userId, Map<Long,String> userHeadImageMap) {
        if (userHeadImageMap.containsKey(userId)){
            return userHeadImageMap.get(userId);
        }
        return "";
    }
    /**
     * 翻译部门
     * @param userId
     * @param deptNameMap
     * @return
     */
    default String getDeptName(Long userId, Map<Long,String> deptNameMap) {
        if (deptNameMap.containsKey(userId)){
            return deptNameMap.get(userId);
        }
        return "";
    }

    /**
     * 翻译性别
     * @param sex
     * @return
     */
    default String getSex(Integer sex){
        switch (sex) {
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "未知";
        }
    }

    /**
     * 翻译生日
     * @param birthday
     * @return
     */
    default LocalDateTime getBirthday(String birthday){
        if (StringUtils.isBlank(birthday)){
            return null;
        }
        return LocalDateUtils.yyyymmddToLocalDateTime(birthday);
    }
}
